from django.http import HttpResponse
from django.conf import settings
import json
import logging
import sqlite3
import os

logging.basicConfig(level=logging.DEBUG,format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',datefmt='%a, %d %b %Y %H:%M:%S')

def deleteShell(request):
	if request.method == 'POST':
		try:	
			received_json_data=json.loads(request.body.decode().replace("'", "\""))
			logging.info("\n\n\n\t"+str(received_json_data)+"\n\n")
			oozieShell=received_json_data['oozieShell'].strip()
			
			oozieShell_dir='/export/datamarket/OozieProperties/oozie_project/oozie_shell/upload'
			result={'result':oozieShell+'脚本删除成功','code':'0'}
			result2={'result':'没有此脚本','code':'1'}
			today='2018-11-10'
			if os.path.exists(oozieShell_dir+"/"+oozieShell):
				os.remove(oozieShell_dir+"/"+oozieShell)
				#把sqlite中存储的脚本信息删除
				db_path = os.path.normpath('/export/datamarket/OozieProperties/oozie_project/db.sqlite3')  
				db = sqlite3.connect(db_path) 
				# 通过cursor创建游标
				cursor = db.cursor()
				sql_cmd=  "delete from oozie_shell where oozieShell='{}'".format(oozieShell) 
				cursor.execute(sql_cmd)
				logging.info('删除sqlite中oozieShell信息')
				cursor.close()
				db.commit()
				db.close()
				logging.info("\n\n\n\t"+oozieShell+"脚本已删除\n\n")
				return HttpResponse(json.dumps(result,ensure_ascii=False))
			else:
				logging.info("\n\n\n\t"+str(result2)+"\n\n")
				return HttpResponse(json.dumps(result2,ensure_ascii=False))

		except:
			logging.info('\n\n\n\t输入格式有误\n\n')
			resultError={'result':'请检查格式','code':'2'}
			return HttpResponse(json.dumps(resultError,ensure_ascii=False))
	else:
		pass


